using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using OpenComposite.Repository.Properties;
using OpenComposite.EII.Repository;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class UCBPolicy : RepositoryItemUI
	{
		#region Initialization
		public UCBPolicy(IServiceProvider serviceProvider)
			: base(serviceProvider)
		{
			InitializeComponent();

			init();
		}
		private void init()
		{
			// 
			// itemListRules
			// 
			this.itemListRules = new OpenComposite.EII.RepositoryUIs.UCRepositoryItemList(serviceProvider);
			this.itemListRules.Dock = System.Windows.Forms.DockStyle.Fill;
			this.itemListRules.FolderID = -1;
			this.itemListRules.Location = new System.Drawing.Point(0, 0);
			this.itemListRules.Name = "itemListRules";
			this.itemListRules.Size = new System.Drawing.Size(375, 194);
			this.itemListRules.TabIndex = 0;
			this.tabRules.Controls.Add(this.itemListRules);

			#region Obsolete
			//codePolicy.Seperators.Add(' ');
			//codePolicy.Seperators.Add('\t');
			//codePolicy.Seperators.Add('\n');
			//codePolicy.Seperators.Add('.');
			//codePolicy.Seperators.Add(',');
			//codePolicy.Seperators.Add(';');
			//codePolicy.Seperators.Add('}');
			//codePolicy.Seperators.Add('{');

			//List<string> keywordsBlue = new List<string>(
			//new string[] {"public", "void", "using", "static", "class",
			//"int","float","char","byte","string","object","bool",
			//"new","typeof","in","as","if","else","try","catch","throw","finally",
			//"switch","case","break","private","protected","true","false",
			//"for","foreach","while","do","get","set","value",
			//"null","this","return","#region","#endregion","namespace",
			//"toString","toNumber","toInt","toBool","setRoleId","setOrgUnitId","setUserId",
			//"setManagersOf","setBusinessPartnerRole",
			//"boolean", "contains" , "count" , "floor" , "id" ,"lang" , "last" , "local-name", "name",
			//"namespace-uri" , "normalize-space" , "position" ,"round" ,"starts-with" ,
			//"string-length" , "sum", "bpws:getVariableData", "setResult", "getResult"});
			//foreach ( string keyword in keywordsBlue ) {
			//    codePolicy.HighlightDescriptors.Add(
			//        new HighlightDescriptor(keyword, Color.Blue, null, DescriptorType.Word,
			//                                DescriptorRecognition.WholeWord, true));
			//}
			//codePolicy.HighlightDescriptors.Add(
			//    new HighlightDescriptor("//", Color.DarkGreen, null, DescriptorType.ToEOL,
			//                            DescriptorRecognition.StartsWith, false));
			//codePolicy.HighlightDescriptors.Add(
			//    new HighlightDescriptor("/*", "*/", Color.DarkGreen, null, DescriptorType.ToCloseToken,
			//                            DescriptorRecognition.StartsWith, false));
			//codePolicy.HighlightDescriptors.Add(
			//    new HighlightDescriptor("\"", "\"", Color.Maroon, null, DescriptorType.ToCloseToken,
			//                            DescriptorRecognition.StartsWith, false));
			//codePolicy.HighlightDescriptors.Add(
			//    new HighlightDescriptor("'", "'", Color.Maroon, null, DescriptorType.ToCloseToken,
			//                            DescriptorRecognition.StartsWith, false));
			#endregion
		}
		#endregion

		#region Public Members

		#region Properties
		public override IRepositoryItem RepositoryItem
		{
			get
			{
				save();
				return _policy;
			}
			set
			{
				base.RepositoryItem = value;
				if ( value is BusinessPolicy ) {
					_policy = (BusinessPolicy)value;
					load();
				}
			}
		}

		public override string TypeText
		{
			get { return Resources.sBPolicy; }
		}

		public override string TypeDescription
		{
			get { return ""; }
		}
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private BusinessPolicy _policy = null;
		//private BusinessPolicy.RulePolicy _rules = null;
		//private BusinessPolicy.ActionPolicy _actions = null;
		#endregion

		#region Methods
		private void load()
		{
			if ( _policy == null )
				return;

			_policy.Reload();

			txtName.Text = _policy.Name;
			txtDesc.Text = _policy.Description;
			txtManager.Text = _policy.PolicyManager;

			itemListRules.SetParentRepositoryItem(ItemType.BPolicy, _policy.ID);

			//codePolicy.Clear();
			//codePolicy.AppendText(_policy.PolicyCode);
		}
		private void save()
		{
			if ( _policy == null )
				_policy = new BusinessPolicy(true);

			_policy.Reload();

			_policy.Name = txtName.Text;
			_policy.Description = txtDesc.Text;
			_policy.PolicyManager = txtManager.Text;
		}

		#endregion

		private void txtName_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ( e.KeyChar < (char)32 ) return;
			e.Handled = !Helpers.IsValidCharForName(e.KeyChar);
		}

		#region Event Handlers
		#endregion

		#endregion

		protected override void NotifyPropertyChange(string property, object value)
		{
			if ( property == "Name" && value != null ) {
				txtName.Text = value.ToString();
			}
		}
	}
}
